package com.waitingsnow.common.share.netEaseMicroblog.data
{
	import com.waitingsnow.common.share.bridia.net.oauth.BDOAuthConnection;
	import com.waitingsnow.common.share.netEaseMicroblog.NEMicroblog;
	import com.waitingsnow.common.share.netEaseMicroblog.events.NEMicroblogErrorEvent;
	import com.waitingsnow.common.share.netEaseMicroblog.events.NEMicroblogEvent;
	import com.waitingsnow.common.share.netEaseMicroblog.events.dispatch;
	import com.waitingsnow.core.debug.Logger;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	
	/**
	 * 
	 * @author Tensharp
	 * 
	 * <p>NEMObject是所有接口的基类.负责访问接口以及抛出事件.</p>
	 * 
	 */	
	
	public class NEMObject extends EventDispatcher
	{
        //==========================================================================
        //  Class variables
        //==========================================================================
        
		/**
		 * @private 
		 * 
		 * <p>上级对象.(NEMicroblog)</p>
		 */		
		
		protected var parent:NEMicroblog;
		
		/**
		 * @private 
		 * 
		 * <p>OAuth链接</p>
		 */		
		
		protected var oauth:BDOAuthConnection;
		
		/**
		 * @private 
		 * 
		 * <p>链接动作,对应NEMConfig中的常量名.</p>
		 */		
		
		protected var action:String;
		
		/**
		 * @private 
		 * 
		 * <p>URLLoader</p>
		 */		
		
		protected var loader:URLLoader;
		
		/**
		 * 
		 * <p>创建一个新的NEMObject对象。</p>
		 * 
		 * @param parent 上级对象.(NEMicroblog)
		 * @param oauth OAuth链接
		 * 
		 */		

		public function NEMObject (parent:NEMicroblog, oauth:BDOAuthConnection)
		{
			this.parent = parent;
			this.oauth  = oauth;
			
			//
			
			initialize ();
		}
		
		/**
		 *
		 * @private 
		 * 
		 */		
		
		private function initialize ():void
		{
			loader = new URLLoader ();
			
			//
			
			loader.addEventListener (Event.COMPLETE, connectCompleteHandler);
			loader.addEventListener (IOErrorEvent.IO_ERROR, connectErrorHandler);
		}
		
		//
		
		/**
		 * 
		 * @private
		 * 
		 * <p>访问接口.</p>
		 * 
		 * @param action 访问接口的动作
		 * @param method http请求方式
		 * @param hasFormat 接口地址是否包含后缀名
		 * @param replace 特殊接口使用 (比如接口url中包含用户id)
		 * 
		 */		
		
		protected function connect (action:String, method:String = "GET", hasFormat:Boolean = true, replace:String = null):void
		{
			this.action  = action;
			
			//
			
			var url:String;
			
			if (replace && replace.length > 0)
			{
				url = uri (replace, hasFormat, replace);
			}
			else
			{
				url = uri (action, hasFormat);
			}
			
			//
			
			loader.load (oauth.getOAuthRequest (method, url));
		}
		
		/**
		 * @private
		 * <p>生成访问接口的URL.</p>
		 * 
		 * 
		 * @param action 访问接口的动作,对应NEMConfig类中的常量名.
		 * @param hasFormat 接口路径是否包含后追名
		 * @param replace 特殊接口使用 (比如接口url中包含用户id)
		 * @return 生成后的URL.
		 * 
		 */		
		
		protected function uri (action:String, hasFormat:Boolean = true, replace:String = null):String
		{
			var string:String;
			
			//
			
			var key:String;
			
			//
			
			if (!replace)
			{
				key = NEMConfig[action];
			}
			else
			{
				key = replace;
			}
			
			if (hasFormat)
			{
				string = NEMConfig.BASE_URL + key + "." + NEMConfig.FORMAT;
			}
			else
			{
				string = NEMConfig.BASE_URL + key;
			}

			//
			
			return string;
		}
        
		/**
		 * 
		 * @private
		 * 
		 * <p>访问成功后抛出事件和返回数据.</p>
		 * 
		 * @param event
		 * 
		 */		
		
		protected function connectCompleteHandler (event:Event):void
		{
			dispatch (parent, NEMicroblogEvent[action + "_COMPLETE"], event.target.data, false);
			Logger.debug(event.target.data);
		}
		
		/**
		 * 
		 * @private
		 * 
		 * <p>访问失败后抛出事件和错误信息.</p>
		 * 
		 * @param event
		 * 
		 */		
		
		protected function connectErrorHandler (event:IOErrorEvent):void
		{
			dispatch (parent, NEMicroblogErrorEvent[action + "_ERROR"], loader.data, true);
			Logger.error(loader.data);
		}
	}
}